﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PermissionsPublicController.cs" company="DTTB">
//   2011
// </copyright>
// <summary>
//   Defines the PermissionsPublicController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Web.Mvc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.PhanQuyen;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Controllers.Base;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.PhanQuyen;
using DTTB.DTO;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.Controllers
{
    public class PermissionsPublicController : BaseController
    {
        public RolesServiceDelegate RolesServiceDelegate = new RolesServiceDelegate();
        public PermissionsPublicServiceDelegate PermissionsPublicServiceDelegate = new PermissionsPublicServiceDelegate();

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult SetPermissionsPublic()
        {
            var form = new PermissionsPublicForm();

            //var dictionary = new Dictionary<string, object> { { "IsDelete", false } };

            ////Get all Permission For all User in PermissionsPublic Table
            form.PermissionsPublicList = this.PermissionsPublicServiceDelegate.GetAllPermissionsPublic();

            ////Get all Paths in database
            form.PathsList = this.PermissionsPublicServiceDelegate.GetPaths();

            ////Build all Paths for User from PermissionsPublic table:
            form.PathsForAllUsersList = new List<PathsDto>();
            for (int i = 0; i < form.PermissionsPublicList.Count; i++)
            {
                for (int j = 0; j < form.PathsList.Count; j++)
                {
                    if (form.PermissionsPublicList[i].PathId == form.PathsList[j].ID)
                    {
                        form.PathsForAllUsersList.Add(form.PathsList[j]);
                    }
                }
            }

            ////Get all Module in Application base on Paths table
            IList<object> modules = new List<object>();
            modules = this.PermissionsPublicServiceDelegate.GetAllModuleInPaths();
            form.Modules = new List<string>();
            foreach (var module in modules)
            {
                form.Modules.Add((string)module);
            }

            //if (messsage == 1)
            //{
            //    form.message = "Update is completed !";
            //}
            //else if (messsage == 0)
            //{
            //    form.message = "Update is not completed ! Please try again.";
            //}

            return View("PermissionsPublic", form);

            ////return RedirectToAction("SetPermissionsPublic", form);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SetPermissionsPublicUpdate(PermissionsPublicForm form)
        {
            try
            {
                IList<PermissionsPublicDto> dtos = new List<PermissionsPublicDto>();
                string[] newPermissionsPublicString = !string.IsNullOrEmpty(form.NewPermissionsPublicString) ? form.NewPermissionsPublicString.Split('|') : new string[0];
                for (int i = 1; i < newPermissionsPublicString.Length; i++)
                {
                    if (!String.IsNullOrEmpty(newPermissionsPublicString[i]))
                    {
                        string[] modulePermissions = newPermissionsPublicString[i].Split('-');
                        //// query all Paths that has permissiontype and module as modulePermissions
                        if (modulePermissions.Length == 2 && !String.IsNullOrEmpty(modulePermissions[0]) &&
                            !String.IsNullOrEmpty(modulePermissions[1]))
                        {
                            string module = modulePermissions[0].Trim();
                            string permissionType = modulePermissions[1].Trim();
                            IList<PathsDto> pathsDtos =
                                this.RolesServiceDelegate.GetAllPathsByModulesAndPermissionType(module, permissionType);
                            foreach (PathsDto pathsDto in pathsDtos)
                            {
                                var PermissionsPublicDto = new PermissionsPublicDto();
                                PermissionsPublicDto.PathId = pathsDto.ID;
                                PermissionsPublicDto.IsDelete = false;
                                PermissionsPublicDto.CreateDate = DateTime.Now;
                                //PermissionsPublicDto.LastUpdatedDate = DateTime.Now; //Không cần thiết
                                PermissionsPublicDto.UpdateDate = DateTime.Now;
                                PermissionsPublicDto.CreateBy = Convert.ToInt32(Session["USERID"]);
                                PermissionsPublicDto.UpdateBy = Convert.ToInt32(Session["USERID"]);

                                dtos.Add(PermissionsPublicDto);
                            }
                        }
                    }
                }

                this.PermissionsPublicServiceDelegate.UpdatePermissionsPublic(dtos);
                form.message = "Successfully Updated !";
            }
            catch (Exception)
            {
                form.message = "Update Failed, Please try again !";
            }

            //Get data cho View


            ////Get all Permission For all User in PermissionsPublic Table
            form.PermissionsPublicList = this.PermissionsPublicServiceDelegate.GetAllPermissionsPublic();

            ////Get all Paths in database
            form.PathsList = this.PermissionsPublicServiceDelegate.GetPaths();

            ////Build all Paths for User from PermissionsPublic table:
            form.PathsForAllUsersList = new List<PathsDto>();
            for (int i = 0; i < form.PermissionsPublicList.Count; i++)
            {
                for (int j = 0; j < form.PathsList.Count; j++)
                {
                    if (form.PermissionsPublicList[i].PathId == form.PathsList[j].ID)
                    {
                        form.PathsForAllUsersList.Add(form.PathsList[j]);
                    }
                }
            }

            ////Get all Module in Application base on Paths table
            IList<object> modules = new List<object>();
            modules = this.PermissionsPublicServiceDelegate.GetAllModuleInPaths();
            form.Modules = new List<string>();
            foreach (var module in modules)
            {
                form.Modules.Add((string)module);
            }

            //End get Data


            return View("PermissionsPublic", form);
        }

    }
}
